import Taro, { useLaunch, getCurrentInstance } from "@tarojs/taro";
import { Provider } from "react-redux";
import configStore from "./store";
import { fetchWechatAuth } from "@/server/login";
import { wechatAuth, fetchStorage } from "@/utils/utils";
import "./app.scss";
const store = configStore();
function App({ children }) {
  useLaunch(() => {
    fetchNetwork();
    fetchCheckUpdate();
    loginChecks();
  });

  const fetchNetwork = () => {
    if (Taro.onNetworkStatusChange) {
      Taro.onNetworkStatusChange((res) => {
        const { isConnected, networkType } = res;
        if (!isConnected) {
          Taro.showToast({
            title: "网络信号不稳定,请检查您的网络",
            duration: 2000,
            icon: "none",
          });
        } else {
          if (networkType === "2g" || networkType === "3g") {
            Taro.showToast({
              title: "当前网络信号差",
              duration: 2000,
              icon: "none",
            });
          }
        }
      });
    }
  };
  const fetchCheckUpdate = () => {
    // 判断目前微信版本是否支持自动更新
    if (Taro.canIUse("getUpdateManager")) {
      const updateManager = Taro.getUpdateManager();
      updateManager.onCheckForUpdate((res) => {
        //检测是否有新版本
        if (res.hasUpdate) {
          updateManager.onUpdateReady(() => {
            Taro.showModal({
              title: "更新提示",
              confirmText: "确定",
              showCancel: false,
              content: "新版本已经准备好，需要重新启动",
              success: function (res) {
                if (res.confirm) {
                  // 更新
                  updateManager.applyUpdate();
                }
              },
            });
          });
        }
      });
    } else {
      Taro.showModal({
        title: "提示",
        content:
          "当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。",
      });
    }
  };
  const loginChecks = () => {
    const launchOptions = Taro.getLaunchOptionsSync();
    if (launchOptions.scene === 1007) {
      const data = Taro.getStorageSync("userInfo") || {};
      const { query = {}, path } = launchOptions
      const args = Object.keys(query).map((item) => {
        return `${item}=${query[item]}`;
      })
        .join("&");
      Taro.setStorageSync("userInfo", {
        ...data,
      });
      Taro.reLaunch({
        url: path + (args ? `?${args}` : "")
      })
    } else if (
      launchOptions.scene === 1008 ||
      launchOptions.scene === 1013 ||
      launchOptions.scene === 1044 ||
      launchOptions.scene === 1007 ||
      launchOptions.scene === 1067 ||
      launchOptions.scene === 1011 ||
      launchOptions.scene === 1012 ||
      launchOptions.scene === 1047 ||
      launchOptions.shareTicket
    ) {
      return
    } else {
      wechatAuth((e) => {
        fetchWechatAuth({ code: e, })
          .then((val) => {
            const { userInfo } = val;
            Taro.eventCenter.trigger('callMethod', userInfo)
          })
          .catch((e) => {
          });
      }
      );
    }

  };
  // children 是将要会渲染的页面
  return <Provider store={store}>{children}</Provider>;
}

export default App;
